import 'dart:io';

import 'package:device_info/device_info.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:tender_flutter/constant/api_path.dart';
import 'package:tender_flutter/constant/resource_path.dart';
import 'package:tender_flutter/constant/theme_colors.dart';
import 'package:tender_flutter/http/api_response.dart';
import 'package:tender_flutter/manager/login_manager.dart';
import 'package:tender_flutter/page/login/api/api_login.dart';
import 'package:tender_flutter/router/router.dart';
import 'package:tender_flutter/style/font_style.dart';
import 'package:tender_flutter/utils/regex_util.dart';
import 'package:tender_flutter/utils/screen_adaper.dart';
import 'package:tender_flutter/utils/toast.dart';
import 'package:tender_flutter/utils/utils.dart';
import 'package:tender_flutter/widgets/base_appbar.dart';
import 'package:tender_flutter/widgets/net_image.dart';
import 'package:tender_flutter/widgets/timer_count_down_widget.dart';

class PhoneLoginPage extends StatefulWidget {
  @override
  _PhoneLoginPageState createState() => _PhoneLoginPageState();
}

class _PhoneLoginPageState extends State<PhoneLoginPage> {
  TextEditingController _phoneController = TextEditingController();
  TextEditingController _imageCodeController = TextEditingController();
  TextEditingController _codeController = TextEditingController();

  String mobileFlag = "1";

  /// 开始登录
  void startLogin() {
    String phone = _phoneController.text;
    String imgCode = _imageCodeController.text;
    String code = _codeController.text;

    if (!RegexUtil.isTel(phone)) {
      ToastUtils.show("请输入正确的手机号码");
      return;
    }
    if (isEmpty(imgCode)) {
      ToastUtils.show("请输入图形验证码");
      return;
    }
    if (isEmpty(code)) {
      ToastUtils.show("请输入验证码");
      return;
    }
    ToastUtils.showLoading("");
    LoginManager.phoneLogin(
        phone: phone,
        code: code,
        successCallback: () {
          ToastUtils.dismissLoading();
          LRouter.navigator.pop();
        },
        failCallback: () {
          ToastUtils.dismissLoading();
        });
  }

  /// 发送验证码
  Future _sendCode() async {
    var response = await ApiLogin.sendCode(
        phone: _phoneController.text,
        mobileID: mobileFlag,
        captcha: _imageCodeController.text);
    if (response.status == Status.SUCCESS) {
      ToastUtils.show("验证码已发送");
    }
  }

  Future _getMobileIdentifier() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    if (Platform.isAndroid) {
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      mobileFlag = androidInfo.androidId;
    } else if (Platform.isIOS) {
      IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
      mobileFlag = iosInfo.identifierForVendor;
    }
  }

  @override
  void initState() {
    super.initState();
    _getMobileIdentifier();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ThemeColors.colorWhite,
      resizeToAvoidBottomPadding: false,
      appBar: _initAppBar(),
      body: _initBody(),
    );
  }

  /// 导航
  Widget _initAppBar() {
    return BaseAppBar(
      context,
      Text("登录", style: FonStyle.navTextStyle()),
      backgroundColor: ThemeColors.colorWhite,
    );
  }

  /// 内容体
  Widget _initBody() {
    return Center(
        child: Column(
      // crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        SizedBox(height: ScreenAdapter.height(60)),
        Image.asset(RPath.imgPath + "icon_big_shape_logo.png",
            width: ScreenAdapter.width(230), height: ScreenAdapter.height(230)),
        SizedBox(height: ScreenAdapter.height(80)),
        Container(
            height: ScreenAdapter.height(100),
            width: ScreenAdapter.width(630),
            padding: EdgeInsets.symmetric(horizontal: ScreenAdapter.width(40)),
            decoration: BoxDecoration(
                color: ThemeColors.colorType57,
                borderRadius: BorderRadius.circular(ScreenAdapter.width(60))),
            child: Row(children: [
              Text(
                "手机号码",
                style: TextStyle(
                    color: ThemeColors.colorType1,
                    fontSize: ScreenAdapter.sp(30)),
              ),
              SizedBox(width: ScreenAdapter.width(30)),
              Container(
                  width: 2,
                  height: ScreenAdapter.height(45),
                  color: ThemeColors.colorType64),
              SizedBox(width: ScreenAdapter.width(30)),
              Expanded(
                  child: TextField(
                decoration: InputDecoration(
                    hintText: "输入手机号码", border: InputBorder.none),
                style: TextStyle(
                  color: ThemeColors.colorType4,
                  fontSize: ScreenAdapter.sp(32),
                ),
                keyboardType: TextInputType.number,
                textAlign: TextAlign.left,
                autofocus: true,
                controller: _phoneController,
              )),
            ])),
        SizedBox(height: ScreenAdapter.height(20)),
        Container(
            height: ScreenAdapter.height(100),
            width: ScreenAdapter.width(630),
            padding: EdgeInsets.only(
                left: ScreenAdapter.width(40), right: ScreenAdapter.width(5)),
            decoration: BoxDecoration(
                color: ThemeColors.colorType57,
                borderRadius: BorderRadius.circular(ScreenAdapter.width(60))),
            child: Row(children: [
              Text(
                "图  形  码",
                style: TextStyle(
                    color: ThemeColors.colorType1,
                    fontSize: ScreenAdapter.sp(30)),
              ),
              SizedBox(width: ScreenAdapter.width(30)),
              Container(
                  width: 2,
                  height: ScreenAdapter.height(45),
                  color: ThemeColors.colorType64),
              SizedBox(width: ScreenAdapter.width(30)),
              Expanded(
                  child: TextField(
                decoration: InputDecoration(
                    hintText: "输入图形码", border: InputBorder.none),
                style: TextStyle(
                  color: ThemeColors.colorType4,
                  fontSize: ScreenAdapter.sp(32),
                ),
                // keyboardType: TextInputType.number,
                textAlign: TextAlign.left,
                autofocus: true,
                controller: _imageCodeController,
              )),
              Container(
                  height: ScreenAdapter.height(100),
                  width: ScreenAdapter.width(200),
                  margin: EdgeInsets.only(right: ScreenAdapter.width(15)),
                  child: MyNetImage(
                    radius: BorderRadius.circular(1),
                    cache: false,
                    url: ApiPath.baseApi +
                        ApiPath.imageCode +
                        "?mobileID=${mobileFlag}",
                    fit: BoxFit.contain,
                    imageClickAction: (state) {
                      state.reLoadImage();
                    },
                  ))
            ])),
        SizedBox(height: ScreenAdapter.height(20)),
        Container(
            height: ScreenAdapter.height(100),
            width: ScreenAdapter.width(630),
            padding: EdgeInsets.only(
                left: ScreenAdapter.width(40), right: ScreenAdapter.width(5)),
            decoration: BoxDecoration(
                color: ThemeColors.colorType57,
                borderRadius: BorderRadius.circular(ScreenAdapter.width(60))),
            child: Row(children: [
              Text(
                "验  证  码",
                style: TextStyle(
                    color: ThemeColors.colorType1,
                    fontSize: ScreenAdapter.sp(30)),
              ),
              SizedBox(width: ScreenAdapter.width(30)),
              Container(
                  width: 2,
                  height: ScreenAdapter.height(45),
                  color: ThemeColors.colorType64),
              SizedBox(width: ScreenAdapter.width(30)),
              Expanded(
                  child: TextField(
                decoration: InputDecoration(
                    hintText: "输入验证码", border: InputBorder.none),
                style: TextStyle(
                  color: ThemeColors.colorType4,
                  fontSize: ScreenAdapter.sp(32),
                ),
                keyboardType: TextInputType.number,
                textAlign: TextAlign.left,
                autofocus: true,
                controller: _codeController,
              )),
              TimerCountDownWidget(
                clickCallBack: () {
                  if (isEmpty(_phoneController.text)) {
                    ToastUtils.show("请输入手机号码");
                    return false;
                  }
                  if (isEmpty(_imageCodeController.text)) {
                    ToastUtils.show("请输入图形验证码");
                    return false;
                  }
                  _sendCode();
                  return true;
                },
              )
            ])),
        SizedBox(height: ScreenAdapter.height(100)),
        Container(
          height: ScreenAdapter.height(100),
          width: ScreenAdapter.width(630),
          child: FlatButton(
            color: ThemeColors.colorType6,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(ScreenAdapter.width(60)),
            ),
            child: Text(
              "登录",
              style: TextStyle(
                  color: ThemeColors.colorWhite,
                  fontSize: ScreenAdapter.sp(28)),
            ),
            onPressed: () {
              startLogin();
            },
          ),
        )
      ],
    ));
  }
}
